<template>
  <div class="app-container">
    <!-- 表单 -->
    <Menu-form
      @findList="findList"
      @addDialog="addDialog"
      ref="formInlineRef"
    />

    <!-- 表格 -->
    <Menu-table
      :tableData="tableData"
      @delItem="delItem"
      @editDialog="editDialog"
      :options="options"
      @addDialog="addDialog"
    />

    <!-- 新增和编辑 弹出框 -->
    <Menu-dialog
      ref="ruleFormRef"
      :RuleForm="ruleForm"
      :dialogVisible="dialogVisible"
      :dialogTitle="dialogTitle"
      @addItem="addItem"
      @editItem="editItem"
      :options="options"
      @close="close"
    />
  </div>
</template>

<script>
import api from "@/api/menu";
import MenuForm from "./MenuForm";
import MenuTable from "./MenuTable";
import MenuDialog from "./MenuDialog";

export default {
  components: {
    MenuForm,
    MenuTable,
    MenuDialog
  },
  data() {
    return {
      options: [
        { val: 1, title: "目录" },
        { val: 2, title: "按钮" },
        { val: 3, title: "菜单" }
      ],
      tableData: [],
      current: 1,
      size: 20,
      dialogTitle: "",
      dialogVisible: false,
      ruleForm: {
        name: "",
        remark: "",
        sort: 0,
        status: ""
      },
      rightList: [],
      rightIds: []
    };
  },
  methods: {
    // 获取列表数据
    getList(e) {
      api.getList(e).then(response => {
        if (response.code == 20000) {
          this.tableData = response.data;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.getList();
    },
    // 查询
    findList(e) {
      this.getList(e);
    },
    // 新增 dialog
    addDialog(e) {
      this.dialogVisible = e;
      this.dialogTitle = "新增";
      this.$refs.ruleFormRef.beforeResetForm();
    },
    // 新增 提交
    addItem(e) {
      api
        .addItem(e)
        .then(response => {
          if (response.code == 20000) {
            this.dialogVisible = false;
            this.$message.success(response.message);
            this.getList();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 编辑 dialog
    editDialog(id) {
      this.dialogVisible = true;
      this.dialogTitle = "编辑";
      api.findItem(id).then(response => {
        if (response.code == 20000) {
          this.ruleForm = response.data;
          this.$refs.ruleFormRef.changeRuleForm(this.ruleForm);
        }
      });
    },
    // 新增 编辑 提交
    editItem(e) {
      api
        .editItem(e)
        .then(response => {
          if (response.code == 20000) {
            this.dialogVisible = false;
            this.$message.success(response.message);
            this.getList();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 删除
    delItem(e) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api.delItem(e).then(response => {
            if (response.code == 20000) {
              this.$message.success(response.message);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 关闭 dialog
    close(e){
      this.dialogVisible = e;
    }
  },
  created() {
    this.getList();
  }
};
</script>
